package jagex;

import java.io.IOException;

public class PacketConstruction {

	public void closeStream() {
	}

	public void readInputStream(int length, byte[] abyte0) throws IOException {
		readInputStream(length, 0, abyte0);
	}

	public void formatPacket() {
		if (skip8Offset != 8) {
			packetOffset++;
		}
		int i = packetOffset - packetStart - 2;
		packetData[packetStart] = (byte) (i >> 8);
		packetData[packetStart + 1] = (byte) i;
		if (maxPacketLength <= 10000) {
			int k = packetData[packetStart + 2] & 0xff;
			packetCommandCount[k]++;
			packetCommandLength[k] += packetOffset - packetStart;
		}

		packetStart = packetOffset;
	}

	public void addByte(int i) {
		packetData[packetOffset++] = (byte) i;
	}

	public void addTwo4ByteInts(long l) {
		add4ByteInt((int) (l >> 32));
		add4ByteInt((int) (l & -1L));
	}

	public void add4ByteInt(int i) {
		packetData[packetOffset++] = (byte) (i >> 24);
		packetData[packetOffset++] = (byte) (i >> 16);
		packetData[packetOffset++] = (byte) (i >> 8);
		packetData[packetOffset++] = (byte) i;
	}

	public boolean containsData() {
		return packetStart > 0;
	}

	public void add2ByteInt(int i) {
		packetData[packetOffset++] = (byte) (i >> 8);
		packetData[packetOffset++] = (byte) i;
	}

	public int readByte() throws IOException {
		return readInputStream();
	}

	public int readPacket(byte data[]) {
		packetReadCount++;
		if (maxPacketReadCount > 0 && packetReadCount > maxPacketReadCount) {
			error = true;
			errorText = "time-out";
			maxPacketReadCount += maxPacketReadCount;
			return 0;
		}
		try {
			if (length == 0 && inputStreamAvailable() >= 2) {
				byte abyte1[] = new byte[2];
				readInputStream(2, 0, abyte1);
				length = ((short) ((abyte1[0] & 0xff) << 8) | (short) (abyte1[1] & 0xff)) + 1;
			}
			if (length > 0 && inputStreamAvailable() >= length) {
				readInputStream(length, data);
				int readBytes = length;
				length = 0;
				packetReadCount = 0;
				return readBytes;
			}
		} catch (IOException ioexception) {
			error = true;
			ioexception.printStackTrace();
		}
		return 0;
	}

	public void readInputStream(int length, int offset, byte abyte0[])
			throws IOException {
	}

	public int inputStreamAvailable() throws IOException {
		return 0;
	}

	public void finalisePacket() throws IOException {
		formatPacket();
		writePacket(0);
	}

	public long read8ByteLong() throws IOException {
		long l = read2ByteInt();
		long l1 = read2ByteInt();
		long l2 = read2ByteInt();
		long l3 = read2ByteInt();
		return (l << 48) + (l1 << 32) + (l2 << 16) + l3;
	}

	public int read2ByteInt() throws IOException {
		int i = readByte();
		int j = readByte();
		return i * 256 + j;
	}

	public void addBytes(byte bytes[], int offset, int length) {
		for (int k = 0; k < length; k++) {
			packetData[packetOffset++] = bytes[offset + k];
		}

	}

	public void writePacket(int i) throws IOException {
		if (error) {
			packetStart = 0;
			packetOffset = 3;
			error = false;
			throw new IOException(errorText);
		}
		packetCount++;
		if (packetCount < i) {
			return;
		}
		if (packetStart > 0) {
			packetCount = 0;
			writeToOutputBuffer(packetData, 0, packetStart);
		}
		packetStart = 0;
		packetOffset = 3;
	}

	public void addString(String s) {
		s.getBytes(0, s.length(), packetData, packetOffset);
		packetOffset += s.length();
	}

	public void writeToOutputBuffer(byte abyte0[], int i, int j)
			throws IOException {
	}

	public void createPacket(int i) {
		if (packetStart > (maxPacketLength * 4) / 5) {
			try {
				writePacket(0);
			} catch (IOException ioexception) {
				error = true;
				errorText = ioexception.getMessage();
			}
		}
		if (packetData == null) {
			packetData = new byte[maxPacketLength];
		}
		packetData[packetStart + 2] = (byte) i;
		packetData[packetStart + 3] = 0;
		packetOffset = packetStart + 3;
		skip8Offset = 8;
	}

	public void qb() throws IOException {
		for (int i = readInputStream(); i != 61 && i != -1; i = readInputStream()) {
			;
		}
	}

	public int vb() throws IOException {
		int i = 0;
		boolean flag = false;
		int j;
		for (j = readInputStream(); j < 48 || j > 57;) {
			if (j == 45) {
				flag = true;
			}
			j = readInputStream();
			if (j == -1) {
				throw new IOException("Eof!");
			}
		}

		for (; j >= 48 && j <= 57; j = readInputStream()) {
			i = (i * 10 + j) - 48;
		}

		if (flag) {
			i = -i;
		}
		return i;
	}

	public String wb() throws IOException {
		String s = "";
		boolean flag = false;
		int i;
		for (i = readInputStream(); i < 32 || i == 44 || i == 59 || i == 61;) {
			i = readInputStream();
			if (i == -1) {
				throw new IOException("Eof!");
			}
		}

		if (i == 34) {
			flag = true;
			i = readInputStream();
		}
		for (; i != -1; i = readInputStream()) {
			if (!flag && (i == 44 || i == 61 || i == 59) || flag && i == 34) {
				break;
			}
			s = s + aCharArray536[i];
		}

		return s;
	}

	public int rb() throws IOException {
		int i = 0;
		int j;
		for (j = readInputStream(); (j < 48 || j > 57) && (j < 97 || j > 102)
				&& (j < 65 || j > 70);) {
			j = readInputStream();
			if (j == -1) {
				throw new IOException("Eof!");
			}
		}

		do {
			if (j >= 48 && j <= 57) {
				i = (i * 16 + j) - 48;
			} else if (j >= 97 && j <= 102) {
				i = (i * 16 + j + 10) - 97;
			} else {
				if (j < 65 || j > 70) {
					break;
				}
				i = (i * 16 + j + 10) - 65;
			}
			j = readInputStream();
		} while (true);
		return i;
	}

	public int readInputStream() throws IOException {
		return 0;
	}

	public PacketConstruction() {
		packetOffset = 3;
		skip8Offset = 8;
		errorText = "";
		maxPacketLength = 5000;
		error = false;
	}

	public PacketConstruction(String arg0) {

	}

	protected int length;
	public int packetReadCount;
	public int maxPacketReadCount;
	public int packetStart;
	protected int packetOffset;
	private int skip8Offset;
	public byte packetData[];
	private static int anIntArray521[] = { 0, 1, 3, 7, 15, 31, 63, 127, 255,
			511, 1023, 2047, 4095, 8191, 16383, 32767, 65535, 0x1ffff, 0x3ffff,
			0x7ffff, 0xfffff, 0x1fffff, 0x3fffff, 0x7fffff, 0xffffff,
			0x1ffffff, 0x3ffffff, 0x7ffffff, 0xfffffff, 0x1fffffff, 0x3fffffff,
			0x7fffffff, -1 };
	final int anInt522 = 61;
	final int anInt523 = 59;
	final int anInt524 = 42;
	final int anInt525 = 43;
	final int anInt526 = 44;
	final int anInt527 = 45;
	final int anInt528 = 46;
	final int anInt529 = 47;
	final int anInt530 = 92;
	final int anInt531 = 32;
	final int anInt532 = 124;
	final int anInt533 = 34;
	static char aCharArray536[];
	public static int packetCommandCount[] = new int[256];
	protected String errorText;
	protected int maxPacketLength;
	protected int packetCount;
	public static int packetCommandLength[] = new int[256];
	protected boolean error;
	public static int anInt543;

	static {
		aCharArray536 = new char[256];
		for (int i = 0; i < 256; i++) {
			aCharArray536[i] = (char) i;
		}

		aCharArray536[61] = '=';
		aCharArray536[59] = ';';
		aCharArray536[42] = '*';
		aCharArray536[43] = '+';
		aCharArray536[44] = ',';
		aCharArray536[45] = '-';
		aCharArray536[46] = '.';
		aCharArray536[47] = '/';
		aCharArray536[92] = '\\';
		aCharArray536[124] = '|';
		aCharArray536[33] = '!';
		aCharArray536[34] = '"';
	}
}
